package com.ruoyi.quartz.crawl;

import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.net.Proxies;
import cn.edu.hfut.dmic.webcollector.plugin.net.OkHttpRequester;
import cn.edu.hfut.dmic.webcollector.plugin.rocks.BreadthCrawler;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.business.domain.ProxyIp;
import com.ruoyi.business.service.IProxyIpService;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.quartz.notify.FollowUpAction;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 * 监控京东页面
 */
@Slf4j
public class CrawlJdPage extends BreadthCrawler {
    static ProxyIp currProxyIp;
    static IProxyIpService iProxyIpService = SpringUtils.getBean(IProxyIpService.class);

    // 自定义的请求插件
    // 可以设置随机代理选择器
    public static class MyRequester extends OkHttpRequester {

        Proxies proxies;

        public MyRequester() {
            proxies = new Proxies();
            currProxyIp = iProxyIpService.getFirstValid();
            if(currProxyIp == null){
                FollowUpAction.sendMail(2);
                proxies.add(null);
            }else{
                proxies.addHttpProxy(currProxyIp.getIp(), currProxyIp.getPort());
            }
        }

        @Override
        public OkHttpClient.Builder createOkHttpClientBuilder() {
            return super.createOkHttpClientBuilder()
                    // 设置一个代理选择器
                    .proxySelector(new ProxySelector() {
                        @Override
                        public List<Proxy> select(URI uri) {
                            // 随机选择1个代理
                            Proxy randomProxy = proxies.randomProxy();
                            // 返回值类型需要是List
                            List<Proxy> randomProxies = new ArrayList<>();
                            //如果随机到null，即不需要代理，返回空的List即可
                            if(randomProxy != null) {
                                randomProxies.add(randomProxy);
                            }
                            log.info("当前Proxy_ip:" + randomProxies);
                            return randomProxies;
                        }

                        @Override
                        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
                            log.info("代理IP链接异常");
                            currProxyIp.setStatus(2);
                            iProxyIpService.updateProxyIp(currProxyIp);
                        }
                    });
        }
    }

    public CrawlJdPage(String crawlPath) {
        super(crawlPath, true);

        // 设置请求插件
        setRequester(new MyRequester());

        // 爬取github about下面的网页
        addSeed("https://item.jd.com/100006843627.html");

    }

    public void visit(Page page, CrawlDatums crawlDatums) {
        Elements element = page.select("div[class=itemover-tip]");
        if(element.size() != 0){
            String content = element.first().text();
            if(StrUtil.isEmpty(content) || !StrUtil.contains(content, "已下柜")){
                FollowUpAction.doNotify();
            }else{
                log.info("获取到的Content：" + content);
            }
        }else{
            log.info("出现异常");
            FollowUpAction.sendMail(3);
        }
    }

    public static void main(String[] args) throws Exception {
        CrawlJdPage crawlJdPage = new CrawlJdPage("C:\\crawl\\demo3");
        crawlJdPage.start(1);
    }
}
